import 'package:flutter/material.dart';
import 'package:my_test_demo/IBasePresenter.dart';
import 'package:my_test_demo/IBaseView.dart';
import 'package:my_test_demo/util/LoadingUtils.dart';

abstract class BaseStateWidget<T extends StatefulWidget, P extends IBasePresenter> extends State<T>
    implements IBaseView {
  late P presenter;

  @override
  void initState() {
    super.initState();
    presenter = initPresenter();
  }

  @override
  void dispose() {
    super.dispose();
    presenter.dispose();
  }

  P initPresenter();

  void simulateAsyncOperation(BuildContext context) async {
    // 显示加载框
    showLoadingDialog(context);

    // 模拟异步操作
    await Future.delayed(const Duration(seconds: 3));

    // 隐藏加载框
    hideLoadingDialog(context);
  }


  void showLoading() {
    
  }

  void dismissLoading() {}

}


class LoadingDialog extends StatelessWidget {
  const LoadingDialog({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const AlertDialog(
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          CircularProgressIndicator(),
          SizedBox(height: 16),
          Text("加载中..."),
        ],
      ),
    );
  }
}

void showLoadingDialog(BuildContext context) {
  showDialog(
    context: context,
    barrierDismissible: false,
    builder: (BuildContext context) {
      return const LoadingDialog();
    },
  );
}

void hideLoadingDialog(BuildContext context) {
  Navigator.of(context).pop();
}
